import axios from "axios";
export default {
    async get(scholar) {
        //throw new Error("API demo error")
        // eslint-disable-next-line no-unreachable
        var res;
        let url = 'https://api.dreamhub.moyunbook.xyz/scholar/author/' + scholar +'/';
        await axios.get(url)
            .then(function (response){
                console.log(response.data);
                res = response.data;
            })
        return {
            info: {
                user_id: res.user_id,
                isFollowed: res.is_followed === true,
                isApplied: res.user_id !== '',
                avatar: res.avatar,
                name: res.name,
                position: res.position,
                org: res.org,
                email: res.email,
                work: res.work,
                edu: res.edu,
                bio: res.bio,
            },
            nodes: res.relation_network.persons.map(temp => {
                return {
                    id: temp.name,
                    group: Math.floor((Math.random()*10) + 1)
                }
            }),
            links: res.relation_network.relations.map(temp => {
                return {
                    source: temp.f,
                    target: temp.t,
                    value: temp.w
                }
            }),
            papers: res.paper,
            interests: res.research_interest.data.map(temp => {
                return {
                    value: temp.n,
                    name: temp.t
                }
            }),
            radar: {
                indicator: res.indices.axis.map(temp => {
                    return {
                        name: temp,
                        max: 1
                    }
                }),
                statics: res.indices.statics[0],
                value: res.indices.normalize[0]
            }
        }
    },

    async sort(scholar, text){
        var res;
        await axios
            .post('/api/scholar/author/' + scholar + '/paper/', {
                sort: text
            })
            .then(function (response){
                // console.log(response.data)
                res = response.data;
            })
        return res;
    },

    async follow(scholar){
        await axios
            .post('/api/follow/', {
                follower: scholar,
            }, {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('access')
                }
            })
    },

    async unfollow(scholar){
        await axios
            .post('/api/follow/author/', {
                author_id: scholar,
            }, {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('access')
                }
            })
    },

    async New_one(name){
        var res;
        await axios
            .post('/api/scholar/author/', {
                name: name,
            }, {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('access')
                }
            })
            .then(function (response) {
                console.log(response.data)
                res = response.data
            })
        return res
    }
}

